package cn.seaboot.admin.starter.eventbus;

import cn.seaboot.commons.eventbus.Eventbus;
import cn.seaboot.commons.eventbus.EventbusListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * EventBus自动注册
 *
 * @author Mr.css on 2017年8月21日
 */
@Component
public class EventBusStarter {
    private final Logger logger = LoggerFactory.getLogger(EventBusStarter.class);

    @Resource
    private ApplicationContext applicationContext;

    @PostConstruct
    public void onApplicationEvent() {

        // 恢复普通任务，普通任务是通过注解扫描进行恢复的
        String[] names = applicationContext.getBeanNamesForAnnotation(EventbusListener.class);
        logger.info("[--- Configuration ---]EventBus start...");
        for (String beanName : names) {
            logger.info("EventBus register a listener：" + beanName);
            Eventbus.register(applicationContext.getBean(beanName));
        }
        logger.info("[--- Configuration ---]EventBus started!");
    }
}
